import { values } from '../../libs/runtime';
import $ from '../common';
Page({
  data: {
    usestudent: [],
    certifiedinfo: [],
    paygroupinfo: [],
    user: {},
    courselist: [], //课程数据
    campus: {}, //机构信息
    idx: 0, //当前选择的学员
    paygroupidx:0,//选项一
    multiIndex:[0,0],//选项二
    copyindex:[0,0],
    multiner:'全部课程类目',
    multiArr:[],
    page: 1,
    name: '', //搜索的东西

    category: '',
    classification: '',
    degree: '',
    level_uuid: '',
    tip: '上滑加载更多',
    isSwitch:false,
    noimg:$.noImage
  },
  form:{},
  onLoad(e) {
    const{type='active'}=e
    this.setData({
      zonecode: wx.getStorageSync('addresscode') || '',
      type
    })
    wx.setNavigationBarTitle({
      title: type=='active'?'校园选课':'我的收藏',
    })
    this.getuserstudent()
    if(type=='active'){
      this.getstudents()
      this.getpaygroup()
    }
  },
  getuserstudent() { //获取_用户下认证子女
    const { zonecode} = this.data
    $.get('/user/students/certified', {
      zonecode
    }, res => {
      let user = res.students[this.data.idx]
      this.setData({
        usestudent: res.students,
        user
      })
      if(res.students.length>0)this.getgroups()
    })
  },
  getstudents() { //获取查询课程类目 筛选
    const {zonecode} = this.data
    $.get('/course/category', {
      zonecode
    }, res => {
      let obj={cat1:'', name1:'全部课程类目' }
      let objs={cat2:'', name2:'全部课程类目' }
      let objss=[]
      let list=[]
      res.category.level1.unshift(obj)
      for (const key in res.category.level2) {
        res.category.level2[key].forEach(v=>{
          v.params=key+''+v.cat2
        })
        res.category.level2[key].unshift(objs)
        list.push(res.category.level2[key])
      }
      let json = JSON.parse(JSON.stringify(list).replace(/name2/g,"name1"));
      json.unshift( objss)
      console.log(json);
      [json[4], json[5]] = [json[5], json[4]];
      let arr=[ res.category.level1,json]
      let multiArr=[res.category.level1,json[this.data.multiIndex[1]]]
      this.setData({
        certifiedinfo: arr,
        multiArr,
        cpoyarr:JSON.parse(JSON.stringify(multiArr))
      })
    })
  },
  getpaygroup() { //获取班级级别
    const { zonecode} = this.data
    $.get('/campus/paygroup/classlevel', {zonecode}, res => {
      let obj={
        name:'全部等级',
        uuid:'',
        level:'level',
        time_create:'',
        time_update:''
      }
      res.unshift(obj)
      this.setData({
        paygroupinfo: res
      })
    })
  },
  getgroups() { //_当前学员能选课程和选课时间
    const { courselist, name, page,degree, classification, category, zonecode,level_uuid} = this.data
    let url='/campus/groups/query'
    if(this.data.type=='collect')url='/campus/student/favoritegroups'
    $.get(url, {
      zonecode,
      campus_uuid: this.data.user.campus_uuid,
      category,
      classification,
      degree,
      level_uuid,
      limit: 20,
      name,
      page,
      student_uuid: this.data.user.student_uuid,
      studygrade: this.data.user.studygrade,
    }, res => {
      if (res.groups.length < 20) {
        this.data.tip = '已经到底了'
      } else {
        this.data.tip = '上滑加载更多'
      }
      res.groups.forEach((v, i) => {
        let week = []
        if(v.schdule) Object.entries(v.schdule).forEach((vs, is) => {
          if (vs[0] == 'Mon') week.push('周一')
          if (vs[0] == 'Tue') week.push('周二')
          if (vs[0] == 'Wed') week.push('周三')
          if (vs[0] == 'Thu') week.push('周四')
          if (vs[0] == 'Fri') week.push('周五')
          if (vs[0] == 'Sat') week.push('周六')
          if (vs[0] == 'Sun') week.push('周日')
        })
        v.week = week.toString()
      });
      this.data.page++
      this.setData({
        campus: res.campus,
        courselist: [...courselist, ...res.groups],
        tip: this.data.tip,
        page: this.data.page,
        isSwitch:false
      })
    })
  },
  switchchildren(){//去往学员
    $.navigateTo('/address/switchchildren')
  },
  onShow() {
    if(this.data.isSwitch){
      this.setData({
        user:this.data.usestudent[this.data.idx],
      })
      this.reset()
    }
  },
  pickChange(e){//筛选选择
    const {value}=e.detail
    this.setData({
      paygroupidx:value,
      level_uuid:this.data.paygroupinfo[value].uuid
    })
    this.reset()
  },
  bindMultiPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let ner
    let category=''
    if(e.detail.value[1]>0){
      ner=this.data.certifiedinfo[1][e.detail.value[0]][e.detail.value[1]].name1
      category=this.data.certifiedinfo[1][e.detail.value[0]][e.detail.value[1]].params
    }else{
      ner=this.data.certifiedinfo[0][e.detail.value[0]].name1
    }
    this.setData({
      multiner:ner,
      category
    })
    this.reset()
  },
  bindMultiPickerColumnChange: function (e) {
    // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    if(e.detail.column==0){
      this.data.multiIndex[0]=e.detail.value
      this.data.multiIndex[1]=0
      this.data.multiArr[1]=this.data.certifiedinfo[1][e.detail.value]
    }else{
      this.data.multiIndex[1]=e.detail.value
    }
    this.setData({
      multiIndex:this.data.multiIndex,
      multiArr:this.data.multiArr,
      copyindex:JSON.parse(JSON.stringify(this.data.multiIndex))
    })
  },
  cancelcollect(e){//取消收藏
    const{item}=e.currentTarget.dataset
    $.get('/campus/group/favor/cancel',{
      zonecode:this.data.zonecode,
      campus_uuid:item.campus_uuid,
      group_uuid:item.uuid,
      student_uuid:this.data.user.student_uuid,
    },res=>{
      // this.setData({
      //   page:1,
      //   courselist:[],
      //   tip:'上滑加载更多'
      // })
      // this.getgroups()
      this.reset()
    })
  },
  godetail(e){//去往详情
    const{student_uuid}=this.data.user
    const{uuid}=e.currentTarget.dataset    
    $.navigateTo(`/course/alreadycourse?student_uuid=${student_uuid}&group_uuid=${uuid}&type=collection`)
  },
  onChange(e){
    this.setData({
      name:e.detail
    })
  },
  onClick(e){
    this.reset()
  },
  onReachBottom() {},
  reset(){
    this.setData({
      page:1,
      courselist:[],
      tip:'上滑加载更多'
    })
    this.getgroups()
  },
  goadd(){
    $.navigateTo('/address/childrenlist')
  }
})